﻿--[[
@file   Offline dialog.
@author jameszhai
@date   2017-04-15
--]]

-- 界面控件
EOfflineUI = 
{
    sDlgTPF = "offline\\DlgOffline.tpf";

    -- 用户信息区
    -- "pnlTitle\\pnlInfo";

    -- 主界面按钮
    sBtnMenu  = "pnlTitle\\pnlSysBtn\\btnConfig";
    sBtnMin   = "pnlTitle\\pnlSysBtn\\btnMin";
    sBtnMax   = "pnlTitle\\pnlSysBtn\\btnMax";
    sBtnRst   = "pnlTitle\\pnlSysBtn\\btnRst";
    sBtnCls   = "pnlTitle\\pnlSysBtn\\btnCls";
    -- sBtnPhone = "pnlTitle\\pnlInfo\\pnlPhone";

};

-- 界面资源配置
EOfflineRes = 
{
};

EMenuItem = 
{
    miStopGame  = 1;
    miOpenDir   = 2;
    miDelGame   = 3;
    miUninstall = 4;

};

TOfflineView = 
{
    InitView = function()
        -- Offline
        ierd_tgp.tpf_ui.Require("TPFLocalization.lua");
        ierd_tgp.tpf_ui.Require("offline\\game_list.lua");

        -- 主界面VIEW
        ierd_tgp.tpf_ui.Require("offline\\navigation_tab.lua");
        ierd_tgp.tpf_ui.Require("offline\\tools_panel.lua");
        ierd_tgp.tpf_ui.Require("offline\\main_menu.lua");

        -- 服务定义
        comm_center.reg_service("Svr_ShowGameMenu", "TOfflineView.OnShowGameMenu");
        comm_center.reg_service("Svr_ShowMainWnd", "TOfflineView.OnShowMainWnd");
        comm_center.reg_service("Svr_ShowBtmMenu", "TOfflineView.OnShowBtmMenu");
        comm_center.listen_broadcast("MsgTGPConfigMgr_UpdateClientMode",
                                     "TOfflineView.on_color_mode_update");
    end;

    UnInitView = function()
        TOfflineView.DestroyUI();
    end;

    -- 设置游戏列表
    SetMyGameList = function(refresh_all_nodes)
        TGMListView.SetMyGameList(refresh_all_nodes);
    end;

    -- 显示界面
    ShowView = function()
        if (not TOfflineView.CreateUI()) then
            TGPLog.LogE("DlgOffline", "Create UI fail.");
            return;
        end;

        TOfflineView.pDlgMain:Restore();
        TOfflineView.pDlgMain:Show(TRUE);
        TOfflineView.pDlgMain:BringToTop();

        TOfflineCtrl.BroadcastShowFinish();
    end;

    -- 隐藏界面
    HideView = function()
        if (nil ~= TOfflineView.pDlgMain) then
            TOfflineView.pDlgMain:Show(FALSE);
        end;
    end;

    -- 设置自己的信息
    SetSelfInfo = function(zInfo)
        if nil == TOfflineView.pDlgMain then
            TGPLog.LogE("DlgOffline", "Set self info, dialog not created.");
            return;
        end;

        local   dwTemp = TOfflineView.pDlgMain:GetControlID();
        local   sNick  = "";

        if nil ~= zInfo["nick"] and "" ~= zInfo["nick"] then
            sNick = zInfo["nick"];
        elseif nil ~= zInfo["suin"] then
            sNick = tostring(zInfo["suin"]);
        else
            TGPLog.LogE("DlgOffline", "Nick and uin are nil.");
        end;

        local   dwNick = ui_utility.get_ctrl_id_by_path(dwTemp,
                                "pnlTitle\\pnlTools\\btnInfo\\btnUserName");

        if TOfflineView.IsValidID(dwNick) then
            ui_utility.setproperty_by_id(dwNick, "Caption", sNick);
            ui_utility.setproperty_by_id(dwNick, "TextTip", sNick);
        end;

        local   sIcon = "ONEIMAGE:";
        if nil ~= zInfo["icon"] and TOfflineView.FileExists(zInfo["icon"]) then
            sIcon = sIcon .. zInfo["icon"];
        else
            sIcon = sIcon .. "login\\icon\\icon_QQ.png";
            TGPLog.LogI("DlgOffline", "Icon is nil.");
        end;
        sIcon = sIcon .. ";Circle;0;"

        local   dwIcon = ui_utility.get_ctrl_id_by_path(dwTemp,
                                "pnlTitle\\pnlInfo\\btnIcon\\ibHead");
        if TOfflineView.IsValidID(dwIcon) then
            ui_utility.setproperty_by_id(dwIcon, "Background", sIcon);
        end;
        tools_panel.set_self_head(sIcon);
    end;

    FileExists = function(sFile)
        if nil == sFile then
            return false;
        end
        local fileSize = ierd_tgp.common.LuaGetFileSize(sFile); -- return -1 when not exists
        if fileSize >= 0 then
            return true;
        end
        return false;
    end;

    -- 创建界面
    CreateUI = function()
        if (nil ~= TOfflineView.pDlgMain) then
            TGPLog.LogI("DlgOffline", "UI dialog already created.");
            return true;
        end;

        local   dwRoot = layout_mgr.restore_layout(EOfflineUI.sDlgTPF);

        if not TOfflineView.IsValidID(dwRoot) then
            TGPLog.LogE("DlgOffline", "Create dialog from XML fail.");
            return false;
        end;

        local   pUILib = ui_utility.uilib();

        if nil == pUILib then
            TGPLog.LogE("DlgOffline", "Get UILib object fail.");
            return false;
        end;

        local   pTemp = pUILib:GetControlPtr(dwRoot);

        TOfflineView.pDlgMain = tolua.cast(pTemp, "Tenio::ITDialog");

        if nil == TOfflineView.pDlgMain then
            TGPLog.LogE("DlgOffline", "Get main dialog fail.");
            return false;
        end;

        -- 调整位置
        local   rtWnd = TOfflineView.pDlgMain:GetRectEx(TRUE,TRUE);
        rtWnd.right  = rtWnd.left + 1280;
        rtWnd.bottom = rtWnd.top  + 830;
        TOfflineView.pDlgMain:MoveEx(rtWnd,TRUE);
        TOfflineView.pDlgMain:MoveToCenter();
        TOfflineView.pDlgMain:BringToTop();
        TOfflineView.pDlgMain:Show(TRUE);

        -- 监听Dialog事件
        ui_utility.setproperty_by_id(dwRoot, "OnDialogRestore", "TOfflineView.OnWndRestore");
        ui_utility.setproperty_by_id(dwRoot, "OnDialogMinimize", "TOfflineView.OnWndMin");
        ui_utility.setproperty_by_id(dwRoot, "OnDialogMaximize", "TOfflineView.OnWndMax");
        ui_utility.setproperty_by_id(dwRoot, "OnDialogClose", "TOfflineView.OnWndClose");

        -- 初始化
        local   dwTemp = ui_utility.get_ctrl_id_by_path(dwRoot, EOfflineUI.sBtnMenu);
        if (TOfflineView.IsValidID(dwTemp)) then
            ui_utility.setproperty_by_id(dwTemp, "OnClick", "TOfflineView.OnMenuClk");
        end;
        
        dwTemp = ui_utility.get_ctrl_id_by_path(dwRoot, EOfflineUI.sBtnMin);
        if (TOfflineView.IsValidID(dwTemp)) then
            ui_utility.setproperty_by_id(dwTemp, "OnClick", "TOfflineView.OnMinClk");
        end;

        dwTemp = ui_utility.get_ctrl_id_by_path(dwRoot, EOfflineUI.sBtnMax);
        if (TOfflineView.IsValidID(dwTemp)) then
            ui_utility.setproperty_by_id(dwTemp, "OnClick", "TOfflineView.OnMaxClk");
            ui_utility.show_ctrl(dwTemp, true);
        end;

        dwTemp = ui_utility.get_ctrl_id_by_path(dwRoot, EOfflineUI.sBtnRst);
        if (TOfflineView.IsValidID(dwTemp)) then
            ui_utility.setproperty_by_id(dwTemp, "OnClick", "TOfflineView.OnRstClk");
            ui_utility.show_ctrl(dwTemp, false);
        end;

        dwTemp = ui_utility.get_ctrl_id_by_path(dwRoot, EOfflineUI.sBtnCls);
        if (TOfflineView.IsValidID(dwTemp)) then
            ui_utility.setproperty_by_id(dwTemp, "OnClick", "TOfflineView.OnClsClk");
        end;

        -- 导航tab
        local nav_tab_panel_id = ui_utility.get_ctrl_id_by_path(dwRoot, "pnlTitle\\pnlTabs");
        navigation_tab.init(nav_tab_panel_id, dwRoot);
        navigation_tab.show(true);

        -- 工具栏
        local tools_panel_id = ui_utility.get_ctrl_id_by_path(dwRoot, "pnlTitle\\pnlTools");
        tools_panel.init(tools_panel_id, dwRoot);
        tools_panel.show(true);

        TOfflineView.nPnlBtm = ui_utility.get_ctrl_id_by_path(dwRoot, "pnlBottom");

        if TOfflineView.IsValidID(TOfflineView.nPnlBtm) then
            TOfflineBtmBar.InitView(TOfflineView.nPnlBtm);
        else
            TGPLog.LogE("OfflineV", "Init offline view fail.");
            return false;
        end;

        TOfflineView.listPanelID = ui_utility.get_ctrl_id_by_path(dwRoot, "pnlClient\\pnlGMList");

        local copyright_id = ui_utility.get_ctrl_id_by_path(dwRoot, "pnlTitle\\stcCopyright");
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            ui_utility.setproperty_by_id(copyright_id, "Visible", "true");
        end
        local color_data_config = comm_center.call_service("SvrTGPConfigMgr_GetColorMode", {});
        if color_data_config ~= nil and color_data_config.color_mode ~=nil then
            TOfflineView.switch_color_mode(color_data_config.color_mode);
        end;
        -- 初始化游戏列表
        return TGMListView.InitView(TOfflineView.listPanelID);
    end;

    -- 销毁界面
    DestroyUI = function()
        if (nil ~= TOfflineView.pDlgMain) then
            TOfflineView.pDlgMain = nil;
        end;
    end;

    -- 菜单按钮
    OnMenuClk = function(pUILib, dwBtnID)
        if nil == TOfflineView.pDlgMain then
            return;
        end
        local dlg_id = TOfflineView.pDlgMain:GetControlID();
        -- 显示菜单
        local menu_id = ui_utility.get_ctrl_id_by_path(dlg_id, "mnShop");
        local btn_tab_id = ui_utility.get_ctrl_parent_id(dwBtnID);
        local btn_tab_ptr = ui_utility.get_ctrl_ptr_by_id(btn_tab_id);
        if nil == btn_tab_ptr  then
            return;
        end
        local btn_tab_rc = btn_tab_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_rc =  TOfflineView.pDlgMain:GetRectEx(TRUE,TRUE);
        local offset_x = btn_tab_rc.left - mainframe_rc.left - TOfflineView.MENU_OFFSET_X;
        local offset_y = btn_tab_rc.top - mainframe_rc.top + TOfflineView.MENU_OFFSET_Y;
        offline_main_menu_view.show(menu_id, true, offset_x, offset_y);
    end;

    OnMainMenuLostFocused = function(pUILib, dwBtnID)
    end;

    -- 最小化按钮
    OnMinClk = function(pUILib, dwBtnID)
        if nil ~= TOfflineView.pDlgMain then
            TOfflineView.pDlgMain:Minimize();
        end;
    end;

    -- 最大化按钮
    OnMaxClk = function(pUILib, dwBtnID)
        if nil ~= TOfflineView.pDlgMain then
            TOfflineView.pDlgMain:Maximize();
        end;
    end;

    -- 恢复按钮
    OnRstClk = function(pUILib, dwBtnID)
        if nil ~= TOfflineView.pDlgMain then
            TOfflineView.pDlgMain:Restore();
        end;
    end;

    -- 关闭按钮
    OnClsClk = function(pUILib, dwBtnID)
        if nil ~= TOfflineView.pDlgMain then
            -- 隐藏主界面
            TOfflineView.pDlgMain:Show(FALSE);

            -- 窗口关闭
            TOfflineView.OnWndClose();

            -- 退出
            local   zAction = comm_center.call_service("Svr_GetCloseAction", {});

            if (nil ~= zAction["exit"]) and (0 ~= zAction["exit"]) then
                ierd_tgp.tpf_ui.ExitTGP();
            end;
        end;
    end;

    -- 窗口恢复
    OnWndRestore = function()
        if nil ~= TOfflineView.pDlgMain then
            local   dwRoot = TOfflineView.pDlgMain:GetControlID();
            local   pTemp  = ui_utility.get_ctrl_ptr_by_path(dwRoot, EOfflineUI.sBtnMax);
            if (nil ~= pTemp) then
                pTemp:Show(TRUE);
            end;

            pTemp = ui_utility.get_ctrl_ptr_by_path(dwRoot, EOfflineUI.sBtnRst);
            if (nil ~= pTemp) then
                pTemp:Show(FALSE);
            end;
        end;
    end;

    -- 最大化
    OnWndMax = function()
        if nil ~= TOfflineView.pDlgMain then
            local   dwRoot = TOfflineView.pDlgMain:GetControlID();
            local   pMaxBtn  = ui_utility.get_ctrl_ptr_by_path(dwRoot, EOfflineUI.sBtnMax);
            if (nil ~= pMaxBtn) then
                pMaxBtn:Show(FALSE);
            end;

            local   pRstBtn = ui_utility.get_ctrl_ptr_by_path(dwRoot, EOfflineUI.sBtnRst);
            if (nil ~= pRstBtn) then
                pRstBtn:Show(TRUE);
            end;
        end;
    end;

    -- 最小化
    OnWndMin = function()
    end;

    -- 点击关闭窗口
    OnWndClose = function()
        local pGMLib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        if nil ~= pGMLib then
            pGMLib:notify_main_wnd_closed();
        end;

        layout_mgr.auto_close_all();
    end;

    -- 是否有效
    IsValidID = function(dwTemp)
        return (nil ~= dwTemp) and (0 ~= dwTemp);
    end;

    -- 显示低栏菜单
    OnShowBtmMenu = function(zParam)
        if (nil == TOfflineView.pDlgMain) then
            TGPLog.LogE("OfflineV", "Show game menu, dialog is null.");
            return;
        end;

        local   nGameID = zParam["GameID"];

        if nil == nGameID or nGameID <= 0 then
            TGPLog.LogE("OfflineV", "Show game menu, game id invalid.");
            return;
        end;

        local   zState = TOfflineModule.GetGameState(nGameID);

        if (nil == zState) then
            TGPLog.LogE("OfflineV", "Show game menu, game state not find.");
            return;
        end;

        if (ierd_tgp.game_library.GAME_STATE_RUNNING ~= zState.iState) then
            TGPLog.LogE("OfflineV", "Game not running, return.");
            return;
        end;

        if nil == TOfflineView.pOPMenu then
            TOfflineView.pOPMenu = ui_utility.get_ctrl_ptr_by_path(
                        TOfflineView.pDlgMain:GetControlID(),
                        "mnGameOP");

            if nil == TOfflineView.pOPMenu then
                TGPLog.LogE("OfflineV", "ShowBtmMenu, Win menu control not find.")
                return;
            end;

            -- 监听事件
            ui_utility.setproperty_by_id(TOfflineView.pOPMenu:GetControlID(), 
                                         "OnWMClickItem",
                                         "TOfflineView.OnMenuItemClk");
        end;

        -- 移除所有菜单项
        TOfflineView.pOPMenu:RemoveAll();
        -- 关闭游戏
        TOfflineView.AddOPMenuItem(LoadStr(ID_CLOSE_GAME),
                                   EMenuItem.miStopGame);
        TOfflineView.nGameID = nGameID;

        local   rtMenu = TOfflineView.pOPMenu:GetRectEx(FALSE,TRUE);
        local   rtTemp = TOfflineView.pDlgMain:GetRectEx(TRUE,TRUE);
        local   nRight = zParam["XOffset"] - rtTemp.left;
        local   nTop   = zParam["YOffset"] - rtTemp.top - 10;

        rtMenu.left   = nRight - (rtMenu.right - rtMenu.left);
        rtMenu.right  = nRight;
        rtMenu.top    = nTop   - (rtMenu.bottom - rtMenu.top);
        rtMenu.bottom = nTop;

        TOfflineView.pOPMenu:MoveEx(rtMenu,TRUE);
        TOfflineView.pOPMenu:Show(TRUE);
    end;

    AddOPMenuItem = function(sText, nIndex)
        local nItemID = 0;

        if nil ~= TOfflineView.pOPMenu then
            local sTemp = U8toCUIStr("Offline\\GMMenuItem.xml");

            nItemID = TOfflineView.pOPMenu:AppendItem(sTemp);

            ui_utility.setproperty_by_id(nItemID, "Caption", sText);

            if nil == TOfflineView.zMenuMap then
                TOfflineView.zMenuMap = {};
            end;

            TOfflineView.zMenuMap[nItemID] = nIndex;
        else
            TGPLog.LogE("OfflineV", "Add menu item, menu control is null.");
        end;

        return nItemID;
    end;

    -- 显示游戏菜单
    OnShowGameMenu = function(zParam)
        if (nil == TOfflineView.pDlgMain) then
            TGPLog.LogE("OfflineV", "Show game menu, dialog is null.");
            return;
        end;

        local   nGameID = zParam["GameID"];

        if nil == nGameID or nGameID <= 0 then
            TGPLog.LogE("OfflineV", "Show game menu, game id invalid.");
            return;
        end;

        local   zState = TOfflineModule.GetGameState(nGameID);

        if (nil == zState) then
            TGPLog.LogE("OfflineV", "Show game menu, game state not find.");
            return;
        end;
        if nil == TOfflineView.pGMMenu then
            TOfflineView.pGMMenu = ui_utility.get_ctrl_ptr_by_path(
                        TOfflineView.pDlgMain:GetControlID(),
                        "WinMenu_GameMenue");

            if nil == TOfflineView.pGMMenu then
                TGPLog.LogE("OfflineV", "Win menu control not find.")
                return;
            end;

            -- 监听事件
            ui_utility.setproperty_by_id(TOfflineView.pGMMenu:GetControlID(), 
                                         "OnWMClickItem",
                                         "TOfflineView.OnMenuItemClk");
        end;

        TOfflineView.nGameID = nGameID;

        -- 移除所有菜单项
        TOfflineView.pGMMenu:RemoveAll();

        local   bEnable = true;     -- 是否在运行

        if (ierd_tgp.game_library.GAME_STATE_RUNNING == zState.iState) then
            bEnable = false;

            -- 关闭游戏
            TOfflineView.AddMenuItem(LoadStr(ID_CLOSE_GAME),
                                     EMenuItem.miStopGame, true);
        end;

        -- 打开游戏目录
        if  ierd_tgp.game_library.GAME_STATE_DOWNLOADING     == zState.iState or
            ierd_tgp.game_library.GAME_STATE_DOWNLOAD_PAUSED == zState.iState or
            ierd_tgp.game_library.GAME_STATE_INSTALLING      == zState.iState or
            ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED   == zState.iState or
            ierd_tgp.game_library.GAME_STATE_DOWNLOADED      == zState.iState or
            ierd_tgp.game_library.GAME_STATE_DOWNLOAD_FAIL   == zState.iState then
            TOfflineView.AddMenuItem(LoadStr(ID_GAME_DIR),
                                     EMenuItem.miOpenDir, false);
        else
            TOfflineView.AddMenuItem(LoadStr(ID_GAME_DIR),
                                     EMenuItem.miOpenDir, true);
        end;

        -- 从游戏列表中移除
        TOfflineView.AddMenuItem(LoadStr(ID_RMV_LIST), 
                                 EMenuItem.miDelGame, bEnable);
        -- 卸载游戏
        TOfflineView.AddMenuItem(LoadStr(ID_UNISTALL), 
                                 EMenuItem.miUninstall, bEnable);

        local   rtMenu = TOfflineView.pGMMenu:GetRectEx(FALSE,TRUE);
        local   rtTemp = TOfflineView.pDlgMain:GetRectEx(TRUE,TRUE);
        local   nLeft  = zParam["XOffset"] - rtTemp.left;
        local   nTop   = zParam["YOffset"] - rtTemp.top;

        rtMenu.right  = nLeft + (rtMenu.right - rtMenu.left);
        rtMenu.left   = nLeft;
        rtMenu.bottom = nTop + (rtMenu.bottom - rtMenu.top);
        rtMenu.top    = nTop;

        TOfflineView.pGMMenu:MoveEx(rtMenu,TRUE);
        TOfflineView.pGMMenu:Show(TRUE);
    end;

    AddMenuItem = function(sText, nIndex, bEnable)
        local nItemID = 0;

        if nil ~= TOfflineView.pGMMenu then
            local sTemp = U8toCUIStr("Offline\\MenuItem.tpf");

            nItemID = TOfflineView.pGMMenu:AppendItem(sTemp);

            ui_utility.setproperty_by_id(nItemID, "Caption", sText);

            if (not bEnable) then
                local   pTemp = ui_utility.get_ctrl_ptr_by_id(nItemID);

                if nil ~= pTemp then
                    pTemp:Enable(FALSE);
                end;
            end;

            if nil == TOfflineView.zMenuMap then
                TOfflineView.zMenuMap = {};
            end;

            TOfflineView.zMenuMap[nItemID] = nIndex;
        else
            TGPLog.LogE("OfflineV", "Add menu item, menu control is null.");
        end;

        return nItemID;
    end;

    -- 菜单点击
    OnMenuItemClk = function(pUILib, nCtrlID, nItemID, nIndex)
        if nil ~= TOfflineView.pGMMenu then
            TOfflineView.pGMMenu:Show(FALSE);
        elseif nil ~= TOfflineView.pOPMenu then
            TOfflineView.pOPMenu:Show(FALSE);
        end;

        local   nType = TOfflineView.zMenuMap[nItemID];

        if nil == nType then
            TGPLog.LogE("OfflineV", "On menu click, item index is nil.");
            return;
        end;

        if 0 == TOfflineView.nGameID then
            TGPLog.LogE("OfflineV", "Click menu item, game id is ZERO.");
            return;
        end;

        local pGMLib = base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");

        if nil == pGMLib then
            TGPLog.LogE("OfflineV", "ClickMenu, Game lib is null.");
            return;
        end;

        if EMenuItem.miStopGame == nType then
            pGMLib:stop_game(TOfflineView.nGameID);
        elseif EMenuItem.miOpenDir == nType then
            pGMLib:open_game_path(TOfflineView.nGameID);
        elseif EMenuItem.miDelGame == nType then
            pGMLib:add_or_remove_game(TOfflineView.nGameID, false);
        elseif EMenuItem.miUninstall == nType then
            pGMLib:uninstall_game(TOfflineView.nGameID);
        else
            TGPLog.LogE("OfflineV", "Unknown menu item:"..tostring(nIndex))
        end;

        TOfflineView.nGameID = 0;
    end;

    -- 更新游戏状态
    UpdateState = function(zState)
        TGMListView.UpdateState(zState);
        TOfflineBtmBar.UpdateState(zState);
    end;

    SetGameState = function(zList)
        TGMListView.SetAllState(zList);
    end;

    -- 显示主界面
    OnShowMainWnd = function(zParam)
        if 0 ~= zParam["show"] then
            TOfflineView.ShowView();
        else
            TOfflineView.HideView();
        end;
    end;

    OnWndPosChange = function(pUILib, nCtrlID)
        if nil ~= TOfflineView.pDlgMain then
            local rc = TOfflineView.pDlgMain:GetRectEx(FALSE,TRUE);
            local cur_w = rc.right - rc.left;
            local cur_h = rc.bottom - rc.top;
            if TOfflineView.ELastMainWndPos.width ~= cur_w or
                TOfflineView.ELastMainWndPos.height ~= cur_h then
                event_center.send_event(mainframe_event.WINDOW_POSITION_CHANGE, "");
            end
            if rc.left ~= rc.right then --不是最小化状态
                TOfflineView.SaveWndPos(rc);
            end
        end
    end;

    SaveWndPos = function(rect)
        TOfflineView.ELastMainWndPos.x = rect.left;
        TOfflineView.ELastMainWndPos.y = rect.top;
        TOfflineView.ELastMainWndPos.width = rect.right - rect.left;
        TOfflineView.ELastMainWndPos.height = rect.bottom - rect.top;
    end;

    HideLoginView = function()
        local uilib = ui_utility.uilib();
        local nLoginDlg = uilib:GetControlIDbyCustomID(CID_LOGIN_DLG);
        if nLoginDlg > 0 then
            local pControl = uilib:GetControlPtr(nLoginDlg);
            local pLoginDlg = tolua.cast(pControl, "Tenio::ITDialog");
            pLoginDlg:Show(FALSE);
            base_utility.log("[offline][HideLoginView]hide login dlg");
        else
            base_utility.log("[offline][HideLoginView]cannot find login dlg");
        end;
    end;

    set_list_panel_visible = function (is_show)
        local pPanel = ui_utility.get_ctrl_ptr_by_id(TOfflineView.listPanelID);

        if nil ~= pPanel then
            if is_show then
               pPanel:Show(TRUE);
            else
               pPanel:Show(FALSE);
            end;
        end;

        TGMListView.OnPanelShow(is_show);
    end;
    
    on_color_mode_update = function(data)
        if nil == data or nil == data.color_mode then
            return;
        end
        TOfflineView.switch_color_mode(data.color_mode);
    end;

    switch_color_mode = function(color_mode)
        base_utility.log("[offline]switch_color_mode: " .. tostring(color_mode), true);
        if TOfflineView.pDlgMain == nil then
            return;
        end;
        if TOfflineView.cur_color_mode_~= nil and
            TOfflineView.cur_color_mode_ == color_mode then
            return;
        end;
        local   dwRoot = TOfflineView.pDlgMain:GetControlID();
        TOfflineView.cur_color_mode_ = color_mode;
        -- TODO(amadeusyu): change tpf style
        local bottom_bar_id = ui_utility.get_ctrl_id_by_path(dwRoot, "pnlBottom");
        local web_area = ui_utility.get_ctrl_id_by_path(dwRoot, "pnlClient\\Ctn_WebArea");

        if bottom_bar_id == nil or web_area == nil then
            base_utility.LogE("[offline]switch_color_mode no bottom bar");
            return;
        end;

        local sText;
        if color_mode == MainCommon.color_mode.kLight then
            sText = "light";
        else
            sText = "dark";
        end;
        local const_ui_str2, str_holder2 = U8toCUIStr(sText);
        local uilib = ui_utility.uilib();
        uilib:ChangeSkinMode(const_ui_str2);
        ui_utility.setproperty_by_id(bottom_bar_id, "SkinMode", color_mode);
        if color_mode == MainCommon.color_mode.kLight then
            ui_utility.setproperty_by_id(bottom_bar_id, "Background", 
                                         TOfflineView.FRAME_BOTTOM_LIGHT_COLOR);

            ui_utility.setproperty_by_id(web_area, "Background", 
                                    TOfflineView.WEB_AREA_LIGHT_COLOR);
        else
            ui_utility.setproperty_by_id(web_area, "Background", 
                                         TOfflineView.WEB_AREA_DARK_COLOR);
            ui_utility.setproperty_by_id(bottom_bar_id, "Background", 
                                         TOfflineView.FRAME_BOTTOM_DARK_COLOR);
        end;

        sText = "Common.xml/BottomBarButton/NormalBtnStyle";
        if color_mode == MainCommon.color_mode.kLight then
            sText = "Common.xml/BottomBarButton/NormalBtnLightStyle";
        end;

        ui_utility.setproperty_by_path(bottom_bar_id, "uoBottom\\pnlBtmBar\\btnDLCCfg",
                "StylePath", sText);
        ui_utility.setproperty_by_path(bottom_bar_id, "uoBottom\\pnlBtmBar\\btnRailCfg",
                "StylePath", sText);
    end;

    -- 界面相关
    pDlgMain = nil;     -- 主界面
    listPanelID = 0;    -- 游戏列表
    nPnlBtm  = 0;       -- 底栏

    -- 菜单相关
    pGMMenu  = nil;
    zMenuMap = nil;
    nGameID  = 0;

    -- 游戏菜单
    pOPMenu  = nil;     -- 游戏操作菜单

    MENU_OFFSET_X = 30;
    MENU_OFFSET_Y = 22;

    ELastMainWndPos = {
        x = 0;
        y = 0;
        width = 0;
        height = 0;
    };

    cur_color_mode_ = nil;

    FRAME_BOTTOM_DARK_COLOR = "COLOR:0x4d4d52;";
    FRAME_BOTTOM_LIGHT_COLOR = "COLOR:0xffffff;";
    WEB_AREA_DARK_COLOR = "COLOR:RGB:61,61,65;";
    WEB_AREA_LIGHT_COLOR = "COLOR:0xeeeeee";

};